package com.mvc;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.mvc.base.model.Company;
import com.mvc.config.ApplicationConfig;
import com.mvc.framework.FrameworkConstants;
import com.mvc.framework.config.ProjectConfig;
import com.mvc.framework.util.DomainUtils;

/**
 * 
 * @author snowpbx
 * @version V1.0 Create Time: 2012-10-13
 */
public final class ProjectUtils {

	private static String	ROOT_NO	        = "";
	public static String	EMAIL_SPLIT	    = ";";
	public static int	  DEFAULT_NO_LENGTH	= ApplicationConfig.getNoLength();

	static {
		for (int i = 0; i < ApplicationConfig.getNoLength(); i++) {
			ROOT_NO += "0";
		}
	}

	public static String toNo(String prefix, int id, int length) {
		String orderNO = ROOT_NO + id;
		return prefix + orderNO.substring(orderNO.length() - length);
	}

	public static String toNo(String prefix, int id) {
		return toNo(prefix, id, DEFAULT_NO_LENGTH);
	}

	public static String toNO(int id) {
		String orderNO = ROOT_NO + id;
		return orderNO.substring(orderNO.length() - DEFAULT_NO_LENGTH);
	}

	public static String getOrderNo(int orderId) {
		return ApplicationConfig.getOrderNoPrefix() + toNO(orderId);
	}

	public static String getPurchaseNo(int purchaseId) {
		return ApplicationConfig.getPurchaseNoPrefix() + toNO(purchaseId);
	}

	public static String getCustomerNo(int customerId) {
		return ApplicationConfig.getCustomerNoPrefix() + toNO(customerId);
	}

	public static String getSupplierNo(int supplierId) {
		return ApplicationConfig.getSupplierNoPrefix() + toNO(supplierId);
	}

	public static String getPackingNo(int packingId) {
		return ApplicationConfig.getPackingNoPrefix() + toNO(packingId);
	}

	public static String getSampleNo(int sampleId) {
		return ApplicationConfig.getSampleNoPrefix() + toNO(sampleId);
	}

	public static String getCommercialInvoiceNo(int objectId) {
		return ApplicationConfig.getCommercialInvoiceNoPrefix() + toNO(objectId);
	}

	public static String getBankStatementNo(int objectId) {
		return ApplicationConfig.getBankStatementNoPrefix() + toNO(objectId);
	}

	public static String getExpenseApplyNo(int objectId) {
		return ApplicationConfig.getExpenseApplyNoPrefix() + toNO(objectId);
	}

	public static String getQuotationNo(int objectId) {
		return ApplicationConfig.getQuotationNoPrefix() + toNO(objectId);
	}

	public static String[] getMailTo(String userEmails) {
		if (StringUtils.isNotBlank(userEmails)) {
			userEmails = userEmails.replace("；", ProjectUtils.EMAIL_SPLIT).replace(",", ProjectUtils.EMAIL_SPLIT)
			        .replace("，", ProjectUtils.EMAIL_SPLIT);
			List<String> list = new ArrayList<String>();
			for (String s : userEmails.split(ProjectUtils.EMAIL_SPLIT)) {
				if (StringUtils.isNotBlank(s)) {
					list.add(s);
				}
			}
			return (String[]) list.toArray(new String[list.size()]);
		}
		return null;
	}

	public static String getPdfHeader(Company company) {
		return null;

	}

	public static String getPdfFooter(Company company) {
		if (company != null) {
			return "<div style='font-size:10px'>Add:" + company.getAddress()
			        + "<div style='float:right;'><pagenumber>/<pagecount></div><br/>Website:" + company.getSite() + " Tel:"
			        + company.getLinkTelephone() + " Mail:" + company.getEmail() + "</div>";
		} else {
			return "<div style='float:right;'><pagenumber>/<pagecount></div>";
		}
	}

	public static String getCNNoReplyMessage() {
		return "此邮件是系统自动发出，请勿回复到此邮件地址，如需帮助，请联系相关联系人。";
	}

	public static String getENNoReplyMessage() {
		return "This message is automatically generated, please do not reply back.";
	}

	public static String getCommonNoReplyMessage() {
		return "<div style='width:80%;text-align:right;margin-right:100px;'>(此邮件为自动发送，请勿直接回复，如有问题请洽管理人员)</div>";
	}

	public static String getParagraphIndent() {
		return "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
	}

	public static String getFullCssThemePath() {
		return DomainUtils.getStaticDomain() + FrameworkConstants.STYLE_ROOT + ProjectConfig.getDefaultSkin() + "/";
	}

	public static String replaceReturn(String in) {
		return in.replace("\n", "").replace("\r", "").replace("\t", "");
	}

	public static String toJsonField(String in) {
		return in.replaceAll("\"", "\\\\\"");
	}
}
